<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Eshop - Ecommerce website</title>

    <!-- === CSS === -->
    <link rel="stylesheet" href="bootstrap-4.0.0-beta/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/ionicons.css">
    <link rel="stylesheet" href="css/now-ui-kit.css">
    <link rel="stylesheet" href="css/app.css">

</head>
<body onload="getPage(0);check()">
<!--=== navbar ===-->
<header class="header">
    <nav class="navbar navbar-expand-sm">
        <div class="container">
            <a class="navbar-brand" href="index.html">EShop</a>
            <button class="navbar-toggler navbar-toggler" type="button" data-toggle="collapse" data-target="#example-navbar" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-bar bar1"></span>
                <span class="navbar-toggler-bar bar2"></span>
                <span class="navbar-toggler-bar bar3"></span>
            </button>
            <div class="collapse navbar-collapse" id="example-navbar" data-nav-image="assets/img/blurred-image-1.jpg">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="index.html">首页</a>
                    </li>
                    <li class="nav-item active dropdown">
                        <a class="nav-link dropdown-toggle" href="http://localhost" id="navbarDropdownMenuLink1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">商城</a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                            <a class="dropdown-item" href="books.html">纸质书</a>
                            <a class="dropdown-item" href="book.html">电子书</a>
                        </div>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">用户</a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                            <a class="dropdown-item" onclick="check()" href="#" id="login">登陆</a>
                            <a class="dropdown-item" href="cart.html" id="cart">购物车</a>
                            <a class="dropdown-item" href="order.html" id="order">订单管理</a>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>
    <!--=== home 2 ===-->
    <section id="home-2" class="header-intro-1 d-flex align-items-center">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="header-intro-wrap">
                        <div class="header-intro-tag"><input type="text" id="searchKey"></div>
                        <div><a onclick="doSearch()" class="btn btn-primary main-btn bg-main">搜索</a></div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!--=== features ===-->
    <section class="features">
        <div class="container" id="container">
            <div class="row">
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap">
                        <div><a href="#">书名</a></div>
                    </div>
                </div>
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap">
                        <div><a href="#">作者</a></div>
                    </div>
                </div>
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap">
                        <div><a href="#">简介</a></div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 分页 -->
        <div class="container" id="pagination">
            <div class="row">
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap"><a href="#" onclick="getPage(-1)">下一页</a></div>
                </div>
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap">
                        <a href="#" id="pageNum">1</a>
                    </div>
                </div>
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="feature-wrap"><a href="#" onclick="getPage(1)">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        下一页
                    </a></div>
                </div>
            </div>
        </div>
    </section>

    <!--=== footer ===-->
    <section class="footer">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="footer-wrap text-center">
                        <div class="footer-tag">All rights Reserved EwenTsai</div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <script type="text/javascript">
        function doSearch() {
            var container = document.getElementById("container");
            var searchKey = document.getElementById("searchKey").value;
            //搜索结果父标签
            var searchResult = document.createElement("div");
            searchResult.id = "searchResult";

            //创建ajax对象
            var ajax;
            if (window.XMLHttpRequest)
            {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                ajax=new XMLHttpRequest();
            }
            else
            {
                // IE6, IE5 浏览器执行代码
                ajax=new ActiveXObject("Microsoft.XMLHTTP");
            }

            ajax.onreadystatechange = function () {
                if(ajax.readyState===4 && ajax.status===200){
                    //删除上一次搜索的结果
                    var deleteResult = document.getElementById("searchResult");
                    if(deleteResult!=null){
                        deleteResult.remove();
                    }

                    // var jsonObj = eval(ajax.responseText);
                    var jsonObj = JSON.parse(ajax.responseText);
                    for (var i in jsonObj){
                        var div_row = document.createElement('div');
                        var div_intro = document.createElement('div');
                        var a_bookname = document.createElement('a');
                        var div_author = document.createElement('div');

                        div_row.className = 'row';
                        a_bookname.className = 'col-4 offset-3';
                        div_author.className = 'col-4';
                        div_intro.className = 'col-4';
                        //单行文本溢出省略
                        div_intro.style = 'overflow: hidden;' +
                            'text-overflow:ellipsis;' +
                            'white-space: nowrap;';

                        div_row.appendChild(a_bookname);
                        div_row.appendChild(div_author);
                        div_row.appendChild(div_intro);

                        a_bookname.innerHTML = jsonObj[i].bookname;
                        div_author.innerHTML = jsonObj[i].author;
                        div_intro.innerHTML = jsonObj[i].intro;
                        a_bookname.href = "book.html?id=" + jsonObj[i].id;

                        searchResult.appendChild(div_row);
                        container.appendChild(searchResult);
                    }
                }
            }
            ajax.open("get","/BTS/search.do?searchText="+searchKey);
            ajax.send(null);
        }
    </script>

    <script type="text/javascript">
        //分页代码
        function getPage(isNextPage) {
            var container = document.getElementById("container");
            var searchKey = document.getElementById("searchKey").value;
            //搜索结果父标签
            var searchResult = document.createElement("div");
            searchResult.id = "searchResult";

            //创建ajax对象
            var ajax;
            if (window.XMLHttpRequest)
            {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                ajax=new XMLHttpRequest();
            }
            else
            {
                // IE6, IE5 浏览器执行代码
                ajax=new ActiveXObject("Microsoft.XMLHTTP");
            }

            ajax.onreadystatechange = function () {
                if(ajax.readyState===4 && ajax.status===200){

                    //删除上一次搜索的结果
                    var deleteResult = document.getElementById("searchResult");
                    if(deleteResult!=null){
                        deleteResult.remove();
                    }

                    var jsonObj = eval('(' + ajax.responseText + ')');
                    for (var i = 0; i < jsonObj.length; i++) {
                        var div_row = document.createElement('div');
                        var div_intro = document.createElement('div');
                        var a_bookname = document.createElement('a');
                        var div_author = document.createElement('div');

                        div_row.className = 'row';
                        a_bookname.className = 'col-4 offset-3';
                        div_author.className = 'col-4';
                        div_intro.className = 'col-4';
                        //单行文本溢出省略
                        div_intro.style = 'overflow: hidden;' +
                            'text-overflow:ellipsis;' +
                            'white-space: nowrap;';

                        div_row.appendChild(a_bookname);
                        div_row.appendChild(div_author);
                        div_row.appendChild(div_intro);

                        a_bookname.innerHTML = jsonObj[i].bookname;
                        div_author.innerHTML = jsonObj[i].author;
                        div_intro.innerHTML = jsonObj[i].intro;
                        a_bookname.href = "book.html?id=" + jsonObj[i].id;

                        searchResult.appendChild(div_row);
                        container.appendChild(searchResult);
                    }
                }
            }
            ajax.open("get","/BTS/pagination.do?bookClass=newBook&isNextPage="+isNextPage);
            ajax.send(null);
        }
    </script>
    <!-- ==== js ==== -->
    <script src="js/jquery-3.2.1.slim.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="bootstrap-4.0.0-beta/js/bootstrap.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/util.js"></script>
</body>
</html>